'use strict';

const debug = require('debug')('egg-passport-gitee');
const assert = require('assert');
const { Strategy } = require('passport-gitee');

module.exports = app => {
  const config = app.config.passportGitee;
  config.passReqToCallback = true;
  assert(config.key, '[egg-passport-gitee] config.passportGitee.key required');
  assert(config.secret, '[egg-passport-gitee] config.passportGitee.secret required');
  config.clientID = config.key;
  config.clientSecret = config.secret;

  app.passport.use('gitee', new Strategy(config, (req, accessToken, refreshToken, params, profile, done) => {
    const user = {
      provider: 'gitee',
      id: profile.id,
      accessToken,
      refreshToken,
      params,
      profile,
    };

    debug('%s %s get user: %j', req.method, req.url, user);

    app.passport.doVerify(req, user, done);
  }));
};
